package visualizer.client.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import visualizer.client.controller.ShowController;
import visualizer.client.event.AlgorithmChangeHandler;
import visualizer.client.event.ResizeEventHandler;
import visualizer.client.utils.AlgorithmType;
import visualizer.client.utils.ScaleInfo;
import visualizer.client.utils.VisualizerUtils;
import visualizer.client.widgets.algoPanel.AlgorithmPanelPresenter;
import visualizer.client.widgets.algoPanel.AlgorithmPanelView;
import visualizer.client.widgets.controlPanel.ControlPanelPresenter;
import visualizer.client.widgets.controlPanel.ControlPanelView;
import visualizer.client.widgets.messageBox.MessageBoxPresenter;
import visualizer.client.widgets.messageBox.MessageBoxView;
import visualizer.client.widgets.tree.TreePresenter;
import visualizer.client.widgets.tree.TreeView;

/**
 * User: DDovzhenko
 * Date: 28.12.13
 * Time: 14:27
 */
public class VisualizerApp extends Composite implements ResizeEventHandler {
    @UiField
    SimplePanel treeContainer;
    @UiField
    SimplePanel controlPanelContainer;
    @UiField
    SimplePanel algoPanelContainer;
    @UiField
    SimplePanel messageBoxContainer;

    private TreePresenter treePresenter;
    private AlgorithmPanelPresenter algoPresenter;
    private ControlPanelPresenter controlPresenter;
    private ShowController showController;
    private MessageBoxPresenter messageBoxPresenter;


    public VisualizerApp() {
        initWidget(ourUiBinder.createAndBindUi(this));
        initComponents();
        ScaleInfo.getInstance().addResizeEventHandler(this);
        updateUI();
        onResize();
    }

    private void initComponents() {
        treePresenter = new TreePresenter(new TreeView(1350, 580));
        treeContainer.setWidget(treePresenter.getWidget());

        algoPresenter = new AlgorithmPanelPresenter(new AlgorithmPanelView());
        algoPresenter.setChangeAlgorithmListener(new AlgorithmChangeHandler() {
            @Override
            public void onAlgoChanged(AlgorithmType newAlgo) {
                if (VisualizerUtils.getInstance().getSelectedAlgorithm() == newAlgo) {
                    return;
                }
                VisualizerUtils.getInstance().setSelectedAlgorithm(newAlgo);
                updateUI();
            }
        });
        algoPanelContainer.setWidget(algoPresenter.getWidget());

        controlPresenter = new ControlPanelPresenter(new ControlPanelView());
        controlPanelContainer.setWidget(controlPresenter.asWidget());

        messageBoxPresenter = new MessageBoxPresenter(new MessageBoxView());
        messageBoxContainer.setWidget(messageBoxPresenter.asWidget());

        showController = new ShowController(controlPresenter, treePresenter, messageBoxPresenter);

        controlPresenter.setEventListener(showController);
    }

    private void updateUI() {
        AlgorithmType selectedAlgorithm = VisualizerUtils.getInstance().getSelectedAlgorithm();
        showController.setAlgorithm(selectedAlgorithm);
        controlPresenter.setAlgorithmType(selectedAlgorithm);
    }

    @Override
    public void onResize() {
        treePresenter.onResize();
        algoPresenter.onResize();
        controlPresenter.onResize();
    }

    //------------- UI Binder --------------
    interface VisualizerAppUiBinder extends UiBinder<HTMLPanel, VisualizerApp> { }
    private static VisualizerAppUiBinder ourUiBinder = GWT.create(VisualizerAppUiBinder.class);
}